﻿using System;
using System.Windows.Forms;
using VideoClub.Business;
using VideoClub.Entities;

namespace VideoClub.UI.Socios
{
    public partial class FrmConsultarSocio : Form
    {
        public FrmConsultarSocio()
        {
            InitializeComponent();
        }

        private void FrmConsultarSocio_Load(object sender, EventArgs e)
        {
            Refrescar();
        }

        public void Refrescar()
        {
            dgvSocios.DataSource = SocioBO.ObtenerTodosLosSocios();
        }

        private void BtnAgregarClick(object sender, EventArgs e)
        {
            var frm = new FrmEditarSocio();
            frm.posicionarAlta();
            frm.FormClosed += new FormClosedEventHandler(frmEditarSocio_FormClosed);
            frm.Show();
        }

        private void frmEditarSocio_FormClosed(object sender, FormClosedEventArgs e)
        {
            Refrescar();
        }

        private void Eliminar_Click(object sender, EventArgs e)
        {

            int IdSocio = Convert.ToInt32(dgvSocios.CurrentRow.Cells[0].Value);

            if (IdSocio != 0)
            {
                DialogResult d = MessageBox.Show("Esta seguro que desea eliminar el socio?", "Eliminar", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
                if (d == DialogResult.Yes)
                {
                    SocioBO.eliminar(IdSocio);
                }
                Refrescar();
            }
            else
            {
                MessageBox.Show("Debe Seleccionar un socio para eliminar.", "Eliminar", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void modificacion()
        {
            int IdSocio = Convert.ToInt32(dgvSocios.CurrentRow.Cells[0].Value);
            var socioAModificar = new Socio();

            socioAModificar = SocioBO.ObtenerSocioPorId(IdSocio);

            var frm = new FrmEditarSocio();
            frm.posicionarModificacion(socioAModificar);
            frm.FormClosed += new FormClosedEventHandler(frmEditarSocio_FormClosed);
            frm.Show();

        }


        private void Modificar_Click(object sender, EventArgs e)
        {
            modificacion();
        }

        private void dgvActores_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            modificacion();
        }


        private void dgvActores_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
            {
                Refrescar();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Close();
        }


    }
}
